from qiniu import Auth, put_file, etag,put_data
import qiniu.config
#需要填写你的 Access Key 和 Secret Key


access_key = 'xTsqSRtbL3hOjyECKsT4ZS8lRmjqOOy65IyhY9hH'
secret_key = '2iNbi-GtrvicS5LlFNPy65ua-8gk8TO2iljsiaZR'
#构建鉴权对象
q = Auth(access_key, secret_key)
#要上传的空间
bucket_name = 'information12'
#上传到七牛后保存的文件名,如果不写由七牛云维护
# key = 'my-python-logo.png';
key =None
#生成上传 Token，可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)
# 封装成一个函数方便调用
def image_storage(image_data):
    #要上传文件的本地路径
    # localfile = './111.jpg'
    # ret, info = put_file(token, key, localfile)
    # print(info)
    # print(ret)
    # assert ret['key'] == key
    # assert ret['hash'] == etag(localfile)
    ret, info = put_data(token, key, image_data)
    # 判断图片是否上传成功
    if info.status_code ==200:
        return ret.get('key')
    else:
        return None

# 测试
if __name__ == '__main__':
    with open('111.jpg','rb') as file:
        print(image_storage(file.read()))
